Completed
Push — master ( 8967bc...e88c19 )
by Rain
02:47
created

Abstract.js ➔ ???   B

Complexity

Conditions 1

Size

Total Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
cc 1
c 13
b 0
f 0
dl 0
loc 89
rs 8.5731

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
2
import window from 'window';
3
import $ from '$';
4
import _ from '_';
5
import key from 'key';
6
7
import {
8
	$win, $html, $doc,
9
	startMicrotime, leftPanelDisabled, leftPanelType,
10
	sUserAgent, bMobileDevice, bAnimationSupported
11
} from 'Common/Globals';
12
13
import {
14
	noop, isNormal, pString, inArray, microtime, timestamp,
15
	detectDropdownVisibility, windowResizeCallback
16
} from 'Common/Utils';
17
18
import {KeyState} from 'Common/Enums';
19
import * as Links from 'Common/Links';
20
import * as Settings from 'Storage/Settings';
21
import * as Events from 'Common/Events';
22
import {initOnStartOrLangChange, initNotificationLanguage} from 'Common/Translator';
23
import {toggle as toggleCmd} from 'Common/Cmd';
24
25
import {routeOff, setHash} from 'Knoin/Knoin';
26
import {AbstractBoot} from 'Knoin/AbstractBoot';
27
28
class AbstractApp extends AbstractBoot
29
{
30
	/**
31
	 * @param {RemoteStorage|AdminRemoteStorage} Remote
32
	 */
33
	constructor(Remote)
34
	{
35
		super();
36
37
		this.googlePreviewSupportedCache = null;
38
		this.isLocalAutocomplete = true;
39
		this.iframe = null;
40
		this.lastErrorTime = 0;
41
42
		this.iframe = $('<iframe class="internal-hiddden" />').appendTo('body');
43
44
		$win.on('error', (event) => {
45
			if (event && event.originalEvent && event.originalEvent.message &&
46
				-1 === inArray(event.originalEvent.message, [
47
					'Script error.', 'Uncaught Error: Error calling method on NPObject.'
48
				]))
49
			{
50
				const time = timestamp();
51
				if (this.lastErrorTime >= time)
52
				{
53
					return;
54
				}
55
56
				this.lastErrorTime = time;
57
58
				Remote.jsError(
59
					noop,
60
					event.originalEvent.message,
61
					event.originalEvent.filename,
62
					event.originalEvent.lineno,
63
					window.location && window.location.toString ? window.location.toString() : '',
64
					$html.attr('class'),
65
					microtime() - startMicrotime
66
				);
67
			}
68
		});
69
70
		$win.on('resize', () => {
71
			Events.pub('window.resize');
72
		});
73
74
		Events.sub('window.resize', _.throttle(() => {
75
			const
76
				iH = $win.height(),
77
				iW = $win.height();
78
79
			if ($win.__sizes[0] !== iH || $win.__sizes[1] !== iW)
80
			{
81
				$win.__sizes[0] = iH;
82
				$win.__sizes[1] = iW;
83
84
				Events.pub('window.resize.real');
85
			}
86
		}, 50));
87
88
// DEBUG
89
//		Events.sub({
90
//			'window.resize': function() {
91
//				window.console.log('window.resize');
92
//			},
93
//			'window.resize.real': function() {
94
//				window.console.log('window.resize.real');
95
//			}
96
//		});
97
98
		$doc.on('keydown', (event) => {
99
			if (event && event.ctrlKey)
100
			{
101
				$html.addClass('rl-ctrl-key-pressed');
102
			}
103
		}).on('keyup', (event) => {
104
			if (event && !event.ctrlKey)
105
			{
106
				$html.removeClass('rl-ctrl-key-pressed');
107
			}
108
		});
109
110
		$doc.on('mousemove keypress click', _.debounce(() => {
111
			Events.pub('rl.auto-logout-refresh');
112
		}, 5000));
113
114
		key('esc, enter', KeyState.All, () => {
115
			detectDropdownVisibility();
116
		});
117
118
		key('ctrl+shift+`', KeyState.All, () => {
119
			toggleCmd();
120
		});
121
	}
122
123
	remote() {
124
		return null;
125
	}
126
127
	data() {
128
		return null;
129
	}
130
131
	getApplicationConfiguration(name, default_) {
132
		return this.applicationConfiguration[name] || default_;
133
	}
134
135
	/**
136
	 * @param {string} link
137
	 * @returns {boolean}
138
	 */
139
	download(link) {
140
141
		if (sUserAgent && (-1 < sUserAgent.indexOf('chrome') || -1 < sUserAgent.indexOf('chrome')))
142
		{
143
			const oLink = window.document.createElement('a');
144
			oLink.href = link;
145
146
			if (window.document && window.document.createEvent)
147
			{
148
				const oE = window.document.createEvent.MouseEvents;
149
				if (oE && oE.initEvent && oLink.dispatchEvent)
150
				{
151
					oE.initEvent('click', true, true);
152
					oLink.dispatchEvent(oE);
153
					return true;
154
				}
155
			}
156
		}
157
158
		if (bMobileDevice)
159
		{
160
			window.open(link, '_self');
161
			window.focus();
162
		}
163
		else
164
		{
165
			this.iframe.attr('src', link);
166
	//		window.document.location.href = link;
167
		}
168
169
		return true;
170
	}
171
172
	/**
173
	 * @returns {boolean}
174
	 */
175
	googlePreviewSupported() {
176
		if (null === this.googlePreviewSupportedCache)
177
		{
178
			this.googlePreviewSupportedCache = !!Settings.settingsGet('AllowGoogleSocial') &&
179
				!!Settings.settingsGet('AllowGoogleSocialPreview');
180
		}
181
182
		return this.googlePreviewSupportedCache;
183
	}
184
185
	/**
186
	 * @param {string} title
187
	 */
188
	setWindowTitle(title) {
189
		title = ((isNormal(title) && 0 < title.length) ? '' + title : '');
190
		if (Settings.settingsGet('Title'))
191
		{
192
			title += (title ? ' - ' : '') + Settings.settingsGet('Title');
193
		}
194
195
		window.document.title = title + ' ...';
196
		window.document.title = title;
197
	}
198
199
	redirectToAdminPanel() {
200
		_.delay(() => {
201
			window.location.href = Links.rootAdmin();
202
		}, 100);
203
	}
204
205
	clearClientSideToken() {
206
		if (window.__rlah_clear)
207
		{
208
			window.__rlah_clear();
209
		}
210
	}
211
212
	/**
213
	 * @param {string} token
214
	 */
215
	setClientSideToken(token) {
216
		if (window.__rlah_set)
217
		{
218
			window.__rlah_set(token);
219
220
			require('Storage/Settings').settingsSet('AuthAccountHash', token);
221
			require('Common/Links').populateAuthSuffix();
222
		}
223
	}
224
225
	/**
226
	 * @param {boolean=} admin = false
227
	 * @param {boolean=} logout = false
228
	 * @param {boolean=} close = false
229
	 */
230
	loginAndLogoutReload(admin = false, logout = false, close = false) {
231
232
		const inIframe = !!Settings.appSettingsGet('inIframe');
233
		let customLogoutLink = pString(Settings.appSettingsGet('customLogoutLink'));
234
235
		if (logout)
236
		{
237
			this.clearClientSideToken();
238
		}
239
240
		if (logout && close && window.close)
241
		{
242
			window.close();
243
		}
244
245
		customLogoutLink = customLogoutLink || (admin ? Links.rootAdmin() : Links.rootUser());
246
247
		if (logout && window.location.href !== customLogoutLink)
248
		{
249
			_.delay(() => {
250
				if (inIframe && window.parent)
251
				{
252
					window.parent.location.href = customLogoutLink;
253
				}
254
				else
255
				{
256
					window.location.href = customLogoutLink;
257
				}
258
			}, 100);
259
		}
260
		else
261
		{
262
			routeOff();
263
			setHash(Links.root(), true);
264
			routeOff();
265
266
			_.delay(() => {
267
				if (inIframe && window.parent)
268
				{
269
					window.parent.location.reload();
270
				}
271
				else
272
				{
273
					window.location.reload();
274
				}
275
			}, 100);
276
		}
277
	}
278
279
	historyBack() {
280
		window.history.back();
281
	}
282
283
	bootstart() {
284
285
		// log('Ps' + 'ss, hac' + 'kers! The' + 're\'s not' + 'hing inte' + 'resting :' + ')');
286
287
		Events.pub('rl.bootstart');
288
289
		const
290
			mobile = Settings.appSettingsGet('mobile'),
291
			ssm = require('ssm'),
292
			ko = require('ko');
293
294
		ko.components.register('SaveTrigger', require('Component/SaveTrigger'));
295
		ko.components.register('Input', require('Component/Input'));
296
		ko.components.register('Select', require('Component/Select'));
297
		ko.components.register('Radio', require('Component/Radio'));
298
		ko.components.register('TextArea', require('Component/TextArea'));
299
		ko.components.register('Date', require('Component/Date'));
300
301
		ko.components.register('x-script', require('Component/Script'));
302
//		ko.components.register('svg-icon', require('Component/SvgIcon'));
303
304
		if (Settings.appSettingsGet('materialDesign') && bAnimationSupported)
305
		{
306
			ko.components.register('Checkbox', require('Component/MaterialDesign/Checkbox'));
307
			ko.components.register('CheckboxSimple', require('Component/Checkbox'));
308
		}
309
		else
310
		{
311
//			ko.components.register('Checkbox', require('Component/Classic/Checkbox'));
312
//			ko.components.register('CheckboxSimple', require('Component/Classic/Checkbox'));
313
			ko.components.register('Checkbox', require('Component/Checkbox'));
314
			ko.components.register('CheckboxSimple', require('Component/Checkbox'));
315
		}
316
317
		initOnStartOrLangChange(initNotificationLanguage);
318
319
		_.delay(windowResizeCallback, 1000);
320
321
		Events.sub('ssm.mobile-enter', () => {
322
			leftPanelDisabled(true);
323
		});
324
325
		Events.sub('ssm.mobile-leave', () => {
326
			leftPanelDisabled(false);
327
		});
328
329
		if (!mobile)
330
		{
331
			ssm.addState({
332
				id: 'mobile',
333
				query: '(max-width: 767px)',
334
				onEnter: () => {
335
					$html.addClass('ssm-state-mobile');
336
					Events.pub('ssm.mobile-enter');
337
				},
338
				onLeave: () => {
339
					$html.removeClass('ssm-state-mobile');
340
					Events.pub('ssm.mobile-leave');
341
				}
342
			});
343
344
			ssm.addState({
345
				id: 'tablet',
346
				query: '(min-width: 768px) and (max-width: 999px)',
347
				onEnter: function() {
348
					$html.addClass('ssm-state-tablet');
349
				},
350
				onLeave: function() {
351
					$html.removeClass('ssm-state-tablet');
352
				}
353
			});
354
355
			ssm.addState({
356
				id: 'desktop',
357
				query: '(min-width: 1000px) and (max-width: 1400px)',
358
				onEnter: () => {
359
					$html.addClass('ssm-state-desktop');
360
				},
361
				onLeave: () => {
362
					$html.removeClass('ssm-state-desktop');
363
				}
364
			});
365
366
			ssm.addState({
367
				id: 'desktop-large',
368
				query: '(min-width: 1401px)',
369
				onEnter: () => {
370
					$html.addClass('ssm-state-desktop-large');
371
				},
372
				onLeave: () => {
373
					$html.removeClass('ssm-state-desktop-large');
374
				}
375
			});
376
		}
377
		else
378
		{
379
			$html.addClass('ssm-state-mobile').addClass('rl-mobile');
380
			Events.pub('ssm.mobile-enter');
381
		}
382
383
		leftPanelDisabled.subscribe((bValue) => {
384
			$html.toggleClass('rl-left-panel-disabled', bValue);
385
			$html.toggleClass('rl-left-panel-enabled', !bValue);
386
		});
387
388
		leftPanelType.subscribe((sValue) => {
389
			$html.toggleClass('rl-left-panel-none', 'none' === sValue);
390
			$html.toggleClass('rl-left-panel-short', 'short' === sValue);
391
		});
392
393
		leftPanelDisabled.valueHasMutated();
394
395
		require('Stores/Language').populate();
396
		require('Stores/Theme').populate();
397
		require('Stores/Social').populate();
398
	}
399
}
400
401
export {AbstractApp, AbstractApp as default};
402